package ch.bfh.mobicomp.soda2.main;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {
	private PreferenceChange preferenceChange;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		Context contextTemperature = null;
		try {
			// Goal: Reach the context of the other app
			// This context is the one presented in the ManifestFile of the
			// requested App:
			// package="ch.bfh.mobicomp.soda2.temperature"
			contextTemperature = createPackageContext(
					"ch.bfh.mobicomp.soda2.temperature",
					Context.CONTEXT_INCLUDE_CODE);

			// Context reached!
			Log.i("SODA_Main",
					"Context: " + contextTemperature.getPackageCodePath());

			// // Goal: Get the class of the other app
			// Class<?> SODATemperatureDataCollectorServiceClass =
			// contextTemperature
			// .getClassLoader()
			// .loadClass(
			// "ch.bfh.mobicomp.soda2.temperature.SODATemperatureDataCollectorService");
			//
			// // Class reached!
			// Log.i("SODA_Main", "Class: " +
			// SODATemperatureDataCollectorServiceClass);
			//
			//
			// // Goal: Get the static Methods of the other app
			// Intent serviceIntent = SODATemperatureDataCollectorService
			// .getUpdateIntervalIntent(contextTemperature, 2);
			// // Method reached!
			// Log.i("SODA_Main", "Broadcast: " +serviceIntent);
			//
			// sendBroadcast(serviceIntent);
			//

			// SODATemperatureDataCollectorService dataCollectorService =
			// (SODATemperatureDataCollectorService)SODATemperatureDataCollectorServiceClass
			// .newInstance();
			//

			// GOAL: Get the sharedPreference from other app
			SharedPreferences sharedPreferencesTemperature = contextTemperature
					.getSharedPreferences(
							"ch.bfh.mobicomp.soda2.temperature.observer.SODATemperatureDataCollectorService.SharedPreferences",
							Context.MODE_PRIVATE);

			// SharedPreferences reached
			Log.i("SODA_Main",
					"SharedPrefs:"
							+ sharedPreferencesTemperature.getString("JSON",
									"unreachable"));
			// Goal: Attach a SharedPreferences Listener to the other app...
			// (Standard procedure)
			preferenceChange = new PreferenceChange();
			sharedPreferencesTemperature
					.registerOnSharedPreferenceChangeListener(preferenceChange);

			// SharedPreferences attached (But will never be called)
			Log.i("SODA_Main", "SharedPrefsChangeManager:" + preferenceChange);

		} catch (NameNotFoundException ex) {
			Log.e("SODA_Main", "Context: Problem: " + ex.toString());
		}
		// } catch (ClassNotFoundException ex) {
		// Log.e("SODA_Main", "Class Access: Problem: " + ex.toString());
		// }

		// } catch (InstantiationException ex) {
		// Log.e("SODA_Main", "Class Access: Problem: " + ex.toString());
		//
		// } catch (IllegalAccessException ex) {
		// Log.e("SODA_Main", "Class Access: Problem: " + ex.toString());
		//
		// }
	}
	
	public PreferenceChange getPreferenceChange() {
		return preferenceChange;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	class PreferenceChange implements OnSharedPreferenceChangeListener {
		public PreferenceChange(){
			Log.i("SODA_Main",
					"SharedPrefsManager: Established...However... it will never be called :-(");
		}
		@Override
		public void onSharedPreferenceChanged(
				SharedPreferences sharedPreferences, String key) {
			Log.i("SODA_Main",
					"SharedPrefs:"
							+ sharedPreferences
									.getString("JSON", "unreachable"));
			String value = sharedPreferences.getString(key, "Nothing");
			Toast.makeText(MainActivity.this, "Changed: JSON: " + value,
					Toast.LENGTH_LONG).show();

		}

	}

}
